﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Reflection;

namespace Hotnet.Web.Utility
{
	public class PaginateHelper
	{
		public int PageSize { get; private set; }
		public int PageNo { get; private set; }
		public int TotalCount { get; private set; }
		public int TotalPages { get; private set; }
		public string Url { get; set; }
		public bool IsHasAnd { get; private set; }
		public int PixCount { get; private set; }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="pageSize"></param>
		/// <param name="totalCount"></param>
		/// <param name="pageIndex"></param>
		/// <param name="url"></param>
		public PaginateHelper(int pageSize, int totalCount, int pageIndex, string url)
			: this(pageSize, totalCount, pageIndex, 5, url) { }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="pageSize"></param>
		/// <param name="totalCount"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pixCount"></param>
		/// <param name="url"></param>
		public PaginateHelper(int pageSize, int totalCount, int pageIndex, int pixCount, string url)
		{
			Url = url;
			PageNo = pageIndex;
			PageSize = pageSize;
			TotalCount = totalCount;
			TotalPages = (int)Math.Ceiling(TotalCount / (double)pageSize);
			TotalCount = totalCount;
			PixCount = pixCount;
		}

		public bool HasPreviousPage
		{
			get { return PageNo > 0; }
		}
		public bool HasNextPage
		{
			get { return PageNo + 1 < TotalPages; }
		}

		/// <summary>
		/// 构造分页实例
		/// </summary>
		/// <param name="totalCount">总记录数</param>
		/// <param name="pageNo">当前页</param>
		/// <param name="pageSize">分页大小</param>
		/// <param name="url">分页的URL</param>
		/// <param name="condition">分页条件类实体</param>
		/// <returns></returns>
		//public static PaginateHelper ConstructPaginate<T>(int totalCount, int pageNo, int pageSize, string url, T searchClassObj) where T: class
		//{
		//    return ConstructPaginate(totalCount, pageNo, pageSize, url, searchClassObj);
		//}


		public static PaginateHelper ConstructPaginate(int totalCount, int pageNo, int pageSize, string url, object searchClassObj)
		{
			if (!url.EndsWith("/"))
				url += "/?";
			else
				url += "?";
			PaginateHelper pHelper = new PaginateHelper(pageSize, totalCount, pageNo, url + ConstructSearchCondition(searchClassObj));
			return pHelper;
		}

		private static string ConstructSearchCondition(object searchClassObj)
		{
			if (searchClassObj == null) return "";
			StringBuilder sb = new StringBuilder();

			Type searchClassType = searchClassObj.GetType();
			PropertyInfo[] propertyInfos = null;
			propertyInfos = searchClassType.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
			foreach (var item in propertyInfos)
			{
				object propertyValue = item.GetValue(searchClassObj, null);
				if (Attribute.GetCustomAttribute(item, typeof(PaginateNotComposeAttribute)) != null || propertyValue == null || string.IsNullOrEmpty(propertyValue.ToString()))
					continue;
				sb.Append(item.Name + "=" + HttpUtility.UrlEncode(propertyValue.ToString()));
				sb.Append("&");
			}

			string temp = sb.ToString();
			if (string.IsNullOrEmpty(temp))
				return "";
			return temp;

		}


	}

	[AttributeUsage(AttributeTargets.Property)]
	public class PaginateNotComposeAttribute : System.Attribute
	{

	}
}
